﻿using System;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace UI
{
    public partial class frmPressureProfile : DevExpress.XtraEditors.XtraForm
    {
        public frmPressureProfile()
        {
            InitializeComponent();
        }
        private float[] dataitemX; //获取X轴数据，用数组保存
        private float[] dataitemY; //获取Y轴数据，用数组保存
        private string chartname = "";  //图标的名字
        private string Xname = "";  //X轴的名字
        private string Yname = "";  //Y轴的名字
        private static Bitmap bm;  //定义一个位图
        private static Graphics gph; //定义gdi绘图
        private void getData()//获取数据
        {
            chartname = "井ID";
            Xname = "地层压力(g/cm³)";
            Yname = "井深(m)";
            dataitemX = new float[20] { 0.7f, 1.29f, 0.91f, 1.44f, 0.11f, 0.03f, 0.16f, 0.19f, 0.48f, 0.04f, 1.7f, 1.29f, 0.91f, 1.44f, 0.11f, 0.03f, 0.16f, 0.19f, 0.48f, 3.4f };
            dataitemY = new float[20] { 123f, 190f, 340f, 490f, 610f, 780f, 900f, 1230f, 1500f, 2000f, 2100f, 3100f, 3300f, 3700f, 4001f, 4301f, 4450f, 5001f, 5070f, 5501f };
        }
        public void Huitu()  //绘图函数
        {
            #region//坐标及轴线参数
            int Xlength = 5 * pictureBox1.Width / 7;//X轴总长
            int Ylength = 5 * pictureBox1.Height / 6;//Y轴总长
            bm = new Bitmap(pictureBox1.Width, pictureBox1.Height);  //设置初始大小
            gph = Graphics.FromImage(bm);  //画在bm上
            gph.Clear(Color.White);  //用白色清空bm
            gph.FillRectangle(new SolidBrush(Color.White), 0, 0, pictureBox1.Width, pictureBox1.Height);//位图的背景色、坐标、高宽
            //设置中心点（坐标原点），中心点名字为cpt
            PointF cpt = new PointF(pictureBox1.Width / 6, pictureBox1.Height / 7);
            //背景色
            gph.FillRectangle(Brushes.LightBlue, 0, 0, pictureBox1.Width, pictureBox1.Height);
            //图表名字
            gph.DrawString(chartname, new Font("宋体", 11), Brushes.Blue, new PointF((bm.Width) / 2 - chartname.Length * 6, 18));
            //X轴
            gph.DrawLine(new Pen(Color.Black, 2), cpt.X, cpt.Y, cpt.X + Xlength + 2, cpt.Y);  //画X轴线
            gph.DrawString(Xname, new Font("宋体", 9), Brushes.Black, new PointF(3 * pictureBox1.Width / 8, pictureBox1.Height / 14)); //X轴名字
            //Y轴
            gph.DrawLine(new Pen(Color.Black, 2), cpt.X, cpt.Y, cpt.X, cpt.Y + Ylength + 2);  //画Y轴线
            DrawString(Yname, new Font("宋体", 10), Brushes.Black, new PointF(cpt.X - pictureBox1.Width / 9, cpt.Y + pictureBox1.Height / 3), -90f); //Y轴名字
            #endregion
            #region//根据数据绘制坐标轴及折线
            //获取X数据最大值
            float Xmaxn = getMax(dataitemX);
            //获取Y数据最大值
            float Ymaxn = getMax(dataitemY);
            //X轴上最大值
            int Xmax = Convert.ToInt32(Xmaxn)+1;
            //Y轴上最大值
            int Ymax = Convert.ToInt32(Ymaxn);
            //获取Y数据最小值
            float Yminn = getMin(dataitemY);
            //Y轴上最小值
            int Ymin;
            //X轴刻度
            string sx="" ;
            for (int i = 0; i <= 10; i++)
            {
                sx = (i * Xmax / 10.0).ToString();
                gph.DrawString(String.Format("{0,1}",sx), new Font("宋体", 11), Brushes.Black, new PointF(cpt.X + (i * Xlength / 10) - sx.ToString().Length * 5, cpt.Y - 20)); //X轴刻度文字
                gph.DrawLine(Pens.Black, cpt.X + (i * Xlength / 10), cpt.Y, cpt.X + (i * Xlength / 10), cpt.Y + Ylength);  //画X轴刻度线
            }
            //Y轴刻度
            string sy = "";
            //Y最大数据大于1000时的Y轴
            if (Ymaxn >= 1000 && Ymaxn <= 8000)
            {
                Ymin = Convert.ToInt32(Yminn / 200) * 200 - 200;
                //Y轴
                for (int i = 0; i <= ((Ymax - Ymin) / 200); i++)
                {
                    sy = (Ymin + (i * 200)).ToString();
                    gph.DrawString(String.Format("{0:N1}", sy), new Font("宋体", 10), Brushes.Black, new PointF(cpt.X - sy.ToString().Length * 10, cpt.Y + (i * 200 * Ylength / (Ymax - Ymin)) - pictureBox1.Height / 100));  //Y轴刻度文字
                    gph.DrawLine(Pens.Black, cpt.X, cpt.Y + (i * 200 * Ylength / (Ymax - Ymin)), cpt.X + Xlength, cpt.Y + (i * 200 * Ylength / (Ymax - Ymin)));  //画Y轴刻度线
                }
                //数据点
                if (dataitemX.Length == dataitemY.Length)
                {
                    for (int i = 1; i <= dataitemX.Length; i++)
                    {
                        //数据点
                        gph.DrawEllipse(Pens.Black, cpt.X + (Xlength * dataitemX[i - 1] / Xmax), cpt.Y + ((dataitemY[i - 1] - Ymin) * Ylength / (Ymax - Ymin)), 1, 1);  //数据点位
                        //折线
                        if (i > 1)
                        {
                            gph.DrawLine(new Pen(Color.Green, 2), cpt.X + (Xlength * dataitemX[i - 2] / Xmax), cpt.Y + ((dataitemY[i - 2] - Ymin) * Ylength / (Ymax - Ymin)), cpt.X + (Xlength * dataitemX[i - 1] / Xmax), cpt.Y + ((dataitemY[i - 1] - Ymin) * Ylength / (Ymax - Ymin)));  //连接所有数据点位形成折线
                        }
                    }
                }
                else
                {
                    MessageBox.Show("输入数据出错！");
                }
            }
            //Y最大数据小于1000时的Y轴
            if ((Ymaxn < 1000 && Ymaxn >= 1) || (Ymaxn > 8000))
            {
                Ymin = Convert.ToInt32(Yminn);
                for (int i = 0; i <= 20; i++)
                {
                    sy = Math.Round(Yminn + i * (Ymax - Yminn) / 20.0, 2).ToString();
                    gph.DrawString(String.Format("{0,6}", sy), new Font("宋体", 10), Brushes.Black, new PointF(cpt.X - sy.ToString().Length * 10, cpt.Y + (i * Ylength / 20) - 5)); //Y轴刻度文字
                    gph.DrawLine(Pens.Black, cpt.X, cpt.Y + (i * Ylength / 20), cpt.X + Xlength, cpt.Y + (i * Ylength / 20));  //画Y轴刻度线
                }
                //数据点
                if (dataitemX.Length == dataitemY.Length)
                {
                    for (int i = 1; i <= dataitemX.Length; i++)
                    {
                        //数据点
                        gph.DrawEllipse(Pens.Black, cpt.X + (Xlength * dataitemX[i - 1] / Xmax), cpt.Y + ((dataitemY[i - 1] - Ymin) * Ylength / (Ymax - Ymin)), 1, 1);  //数据点位
                        //折线
                        if (i > 1)
                        {
                            gph.DrawLine(new Pen(Color.Green, 2), cpt.X + (Xlength * dataitemX[i - 2] / Xmax), cpt.Y + ((dataitemY[i - 2] - Ymin) * Ylength / (Ymax - Ymin)), cpt.X + (Xlength * dataitemX[i - 1] / Xmax), cpt.Y + ((dataitemY[i - 1] - Ymin) * Ylength / (Ymax - Ymin)));  //连接所有数据点位形成折线
                        }
                    }
                }
                else
                {
                    MessageBox.Show("输入数据出错！");
                }
            }
            #endregion
        }
        private float getMin(float[] dataitem)
        {
            ArrayList a = new ArrayList(dataitem);
            a.Sort();
            float[] b = new float[dataitem.Length];
            a.CopyTo(b);
            return b[0];
        }
        private float getMax(float[] dataitem)
        {
            ArrayList a = new ArrayList(dataitem);
            a.Sort();
            float[] b = new float[dataitem.Length];
            a.CopyTo(b);
            return b[dataitem.Length - 1];
        }
        public void DrawString(string s, Font font, Brush brush, PointF point, float angle)//Y轴名字显示
        {
            Matrix mtxSave = gph.Transform;
            Matrix mtxRotate = gph.Transform;
            mtxRotate.RotateAt(angle, point);
            gph.Transform = mtxRotate;
            gph.DrawString(s, font, brush, point);
            gph.Transform = mtxSave;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            getData();
            Huitu();
            pictureBox1.Image = bm;
        }
        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            getData();
            Huitu();
            pictureBox1.Image = bm;
        }
    }
}
